<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="编号" prop="no">
        <el-input
          v-model="queryParams.no"
          placeholder="请输入编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="别名" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入别名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="渠道" prop="channel">
        <el-input
          v-model="queryParams.channel"
          placeholder="请输入渠道"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="其他资质" prop="otherCert">
        <el-input
          v-model="queryParams.otherCert"
          placeholder="请输入其他资质"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['custom:custom:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['custom:custom:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          :loading="exportLoading"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['custom:custom:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button   v-if='this.queryParams.groupType==null'
                     type="primary"
                     plain
                     icon="el-icon-edit"
                     size="mini"
                     :disabled="multiple"
                     @click="handleGet"
                     v-hasPermi="['custom:custom:get']"
        >批量领取</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="customList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <!--      <el-table-column label="编号" align="center" prop="no" />-->
      <el-table-column label="客户" align="center" prop="name" width='150'>
        <template slot-scope="scope">
          <div>
            <el-row>
              <el-col>
                <div style='color:#409EFF;cursor: pointer' @click="goCustomInfo(scope.row.id)" >{{scope.row.name}}</div>
              </el-col>
              <el-col>
                <div><i class="el-icon-mobile-phone"></i>{{scope.row.phone}}</div>
              </el-col>
              <el-col>
                <el-rate  disabled  v-model='scope.row.level'></el-rate>
              </el-col>
            </el-row>
          </div>
        </template>
      </el-table-column>
      <!--      <el-table-column label="电话" align="center" prop="phone" />-->
      <!--      <el-table-column label="别名" align="center" prop="nickName" />-->
      <!--      <el-table-column label="性别" align="center" prop="sex" />-->
      <el-table-column label="渠道" align="center" prop="channel" />
      <!--      <el-table-column label="星级" align="center" prop="level" />-->
      <el-table-column label="分组" align="center" prop="groupType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.custom_group_type" :value="scope.row.groupType"/>
        </template>
      </el-table-column>
      <el-table-column label="资质" align="center" prop="otherCert" width='300'>
        <template slot-scope="scope">
          <el-row>
            <el-col>
              <div v-if="scope.row.sateInfo.length > 0" style="display:flex;flex-wrap:wrap;text-align:center;justify-content:center;">
                <el-tag round size="small" style='margin-left: 5px;' v-for="(tag,idx) in scope.row.sateInfo" :key="idx" > {{tag}}  </el-tag>
              </div>
            </el-col>
            <el-col>
              <div>{{scope.row.otherCert}}</div>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.custom_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="跟进人" align="center" prop="userName" />
      <el-table-column label="最后跟进" align="center" prop="record" width='250' >
        <template slot-scope="scope">
          <div style='color:#409EFF' v-if='scope.row.createBy!=null'>{{scope.row.createBy}} @ {{scope.row.lastTime}}</div>
          <div>{{scope.row.record}}</div>
        </template>
      </el-table-column>
      <el-table-column label="时间" align="center" prop="createTime" width='260'>
        <template slot-scope="scope">
          <div>[入]{{scope.row.createTime}}</div>
          <div style='margin: 5px 0px;'>[派]{{scope.row.followTime}}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <CustomModal ref="customModal" @refresh="getList"/>
    <CustomDetail ref="customDetail" @refresh="getList"/>
    <CustomSend ref='customSend' @refresh="getList"></CustomSend>

  </div>
</template>

<script>
import { listCustom, delCustom, exportCustom, updateCustom, listCustomData, getCustoms } from '@/api/custom/custom'
import CustomModal from './modal';
import CustomDetail from './detail';
import CustomSend from '@v/custom/custom/send.vue'
import { addRecord } from '@/api/custom/record'

export default {
  name: "Custom",
  dicts: ['custom_status','custom_group_type'],
  components: { CustomSend, CustomModal, CustomDetail},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户表格数据
      customList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        no: null,
        name: null,
        phone: null,
        nickName: null,
        sex: null,
        channel: null,
        level: null,
        status: null,
        groupType: null,
        otherCert: null,
        sateInfo: null,
        orderByColumn: "id",
        isAsc: "desc",
        publicPool:2,
      },
    };
  },
  created() {
    this.groupType = this.$route.query.groupType || '';
    console.log("*",this.groupType);
    if(this.groupType){
      this.queryParams.groupType = this.groupType;
    }
    this.getList();
  },
  methods: {
    /** 查询客户列表 */
    getList() {
      this.loading = true;
      listCustomData(this.queryParams).then(response => {
        this.customList = response.rows;
        this.customList.map(item =>{
          item.sateInfo = JSON.parse(item.sateInfo);
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.customModal.handleAdd();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids
      this.$refs.customModal.handleUpdate(id);
    },
    /** 分派按钮操作 */
    handleSend(row) {
      const ids = row.id || this.ids
      const names = row.name || this.names
      this.$refs.customSend.handleSend(ids,names);
    },
    handleGet(row){
      const ids = row.id || this.ids
      const data = {
        ids:ids,
        publicPool:1,
        groupType:1
      }
      this.$modal.confirm('确认领取客户？').then(function() {
        return getCustoms(data);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("领取成功");
      }).catch(() => {});
    },
    /**
     * 抛入公共池
     * @param row
     */
    publicPool(row){
      const data = {
        id:row.id,
        publicPool:2
      }
      this.$modal.confirm('确认把客户抛入公共池？').then(function() {
        return updateCustom(data);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      const id = row.id || this.ids
      this.$refs.customDetail.handleDetail(id);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.names = selection.map(item =>item.name);
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除客户编号为"' + ids + '"的数据项？').then(function() {
        return delCustom(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有客户数据项？').then(() => {
        this.download('custom/custom/export', {
          ...queryParams
        }, `客户_${new Date().getTime()}.xlsx`)
      }).catch(() => {});
    }
  }
};
</script>
